package com.xxx.ware.ware_manage_sys.controller.user;

import com.xxx.ware.ware_manage_sys.bean.constant.BaseConstant;
import com.xxx.ware.ware_manage_sys.bean.dto.LoginTicket;
import com.xxx.ware.ware_manage_sys.bean.dto.UserQueryDto;
import com.xxx.ware.ware_manage_sys.bean.dto.UserSaveUpdateDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WebResultDto;
import com.xxx.ware.ware_manage_sys.bean.vo.ResultVo;
import com.xxx.ware.ware_manage_sys.bean.vo.UserVo;
import com.xxx.ware.ware_manage_sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/wms/user")
@MapperScan("com.xxx.ware.ware_manage_sys.mapper")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/list")
    @ApiOperation(value = "用户列表", notes = "用户列表", httpMethod = "POST")
    public ResultVo<WebResultDto<UserVo>> getUserList(@RequestBody @Validated UserQueryDto userQueryDto){
        try {
            log.info("用户列表，入参：{}", userQueryDto);
            return ResultVo.data(userService.getUserList(userQueryDto));
        } catch (Exception e) {
            log.error("获取用户列表失败，对应失败原因：" + e.getMessage());
            return ResultVo.fail("获取用户列表失败");
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    public ResultVo<Boolean> saveUser(@RequestBody @Validated UserSaveUpdateDto userSaveUpdateDto){
        try {
            return ResultVo.data(userService.saveUser(userSaveUpdateDto));
        } catch (Exception e) {
            log.error("保存用户失败，对应失败信息：" + e.getMessage());
            return ResultVo.fail("保存用户失败");
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户", notes = "更新用户", httpMethod = "POST")
    public ResultVo<Boolean> updateUser(@Validated @RequestBody UserSaveUpdateDto userSaveUpdateDto){
        try {
            return ResultVo.data(userService.updateUser(userSaveUpdateDto));
        } catch (Exception e) {
            log.error("更新管理员信息失败，对应失败原因：" + e);
            return ResultVo.fail("更新管理员信息失败");
        }
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除用户", notes = "删除用户", httpMethod = "GET")
    public ResultVo<Boolean> removeUser(@RequestParam("id") Long id){
        try {
            return ResultVo.data(userService.removeUser(id));
        } catch (Exception e) {
            log.warn("删除管理员失败，对应失败原因：" + e);
            return ResultVo.fail("删除管理员失败");
        }
    }

    @GetMapping("/getUserInfo")
    public ResultVo<UserVo> getUserInfo(@RequestParam("userName") String userName,
                                        @RequestParam("userPwd") String userPwd,
                                        HttpServletResponse response){
        try {
            log.info("获取登录用户信息，用户名：{}，密码：{}", userName, userPwd);
            return ResultVo.data(userService.getUserByNamePwd(userName, userPwd));
        } catch (Exception e) {
            log.warn("查询用户信息异常，异常原因：" + e);
            return ResultVo.fail("查询用户信息异常");
        }
    }

    @GetMapping("/userDetail")
    public ResultVo<UserVo> getUserDetail(@RequestParam("id") Long id){
        try {
            return ResultVo.data(userService.getUserDetail(id));
        } catch (Exception e) {
            log.warn("获取用户信息异常，异常原因：" + e);
            return ResultVo.fail("获取用户信息异常");
        }
    }

}
